package com.uhuibao.tmall.common.core.domain;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

public class XmlResult {
    protected static final Logger logger = LoggerFactory.getLogger(XmlResult.class);
    private static final String XML_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";

    public static String javaBeanToXml(Object obj)
    {
        String xml = "";
        if (Objects.isNull(obj))
        {
            return xml;
        }
        try
        {
            XmlMapper xmlMapper = new XmlMapper();
            xml = xmlMapper.writeValueAsString(obj);
        } catch (Exception e)
        {
            logger.error("javaBeanToXml error, obj={}, xml={}", obj, xml, e);
            return "";
        }
        // 添加xml文件头
        return XML_HEAD + xml;
    }
}
